#include  "cn_usart_x.h"
#include  "wm_gpio_afsel.h"
#include "command_process.h"
#include "wm_timer.h"
//#include "wm_dma.h"

//extern int tls_uart_dma_off(u16 uart_no);

#define CN_UART_TAST_STK_SIZE	512
#define CN_UART_RX_BUF_SIZE	2048

#define  RECV_TIMEOUT                300
static OS_STK   cn_frist_uart_task_stk[CN_UART_TAST_STK_SIZE];
static OS_STK   cn_second_uart_task_stk[CN_UART_TAST_STK_SIZE];
static OS_STK   cn_third_uart_task_stk[CN_UART_TAST_STK_SIZE];
static OS_STK   cn_forth_uart_task_stk[CN_UART_TAST_STK_SIZE];



/**
 * @typedef struct CN_USART
 */
typedef struct CN_USART
{
    tls_os_queue_t *cn_uart_q;
    //    OS_STK *cn_uart_task_stk;
    int bandrate;
    TLS_UART_PMODE_T parity;
    TLS_UART_STOPBITS_T stopbits;
    TLS_UART_CHSIZE_T charlength;
    TLS_UART_FLOW_CTRL_MODE_T  flow_ctrl;
    char *rx_buf;
    int rx_msg_num;
    int rx_data_len;
    u8 com_contorl_flag;//1代表串口打开状态，0代表关闭串口状态
    int recv_overtime;
} CN_USART_ST;

extern s16 uart_tx_sent_callback(struct tls_uart_port *port);

static CN_USART_ST *cn_frist_uart = NULL;
static CN_USART_ST *cn_second_uart = NULL;
static CN_USART_ST *cn_third_uart = NULL;
static CN_USART_ST *cn_forth_uart = NULL;

static u8 usart1_timerout_id = 0;
static u8 usart2_timerout_id = 0;
static u8 usart3_timerout_id = 0;
static u8 usart4_timerout_id = 0;

static int usart1_recv_timeout = RECV_TIMEOUT;
static int usart2_recv_timeout = RECV_TIMEOUT;
static int usart3_recv_timeout = RECV_TIMEOUT;
static int usart4_recv_timeout = RECV_TIMEOUT;

static void usart1_timerout_irq(u8 *arg)
{
  //  printf("usart1_timerout_irq:%d \r\n",cn_frist_uart->rx_data_len);
    recv_usart_package(USART_FIRST_TYPE, (u8 *) cn_frist_uart->rx_buf, cn_frist_uart->rx_data_len);
    cn_frist_uart->rx_data_len = 0;
    tls_timer_stop(usart1_timerout_id);

}

static void usart2_timerout_irq(u8 *arg)
{
 //   printf("usart2_timerout_irq:%d \r\n",cn_second_uart->rx_data_len);
    recv_usart_package(USART_SECOND_TYPE , (u8 *) cn_second_uart->rx_buf, cn_second_uart->rx_data_len);
    cn_second_uart->rx_data_len = 0;
    tls_timer_stop(usart2_timerout_id);
}

static void usart3_timerout_irq(u8 *arg)
{
//    printf("usart3_timerout_irq:%d \r\n",cn_third_uart->rx_data_len);
    recv_usart_package(USART_THIRD_TYPE, (u8 *) cn_third_uart->rx_buf, cn_third_uart->rx_data_len);
    cn_third_uart->rx_data_len = 0;
    tls_timer_stop(usart3_timerout_id);
}

static void usart4_timerout_irq(u8 *arg)
{
//    printf("usart4_timerout_irq:%d \r\n",cn_forth_uart->rx_data_len);
   recv_usart_package(USART_FORTH_TYPE, (u8 *) cn_forth_uart->rx_buf, cn_forth_uart->rx_data_len);
    cn_forth_uart->rx_data_len = 0;
    tls_timer_stop(usart4_timerout_id);
}


//串口1回调
static s16 cn_frist_uart_rx(u16 len)
{
    int ret;
    if (NULL == cn_frist_uart)
    {
        return WM_FAILED;
    }
#if 1

#if 0
    memset(cn_frist_uart->rx_buf, 0, (len + 1));
    ret = tls_uart_read(TLS_UART_1, (u8 *) cn_frist_uart->rx_buf, len);  /* input */
    if (ret <= 0)
    {
        return 0;
    }
    printf("recv_usart1_package:%d,%d \r\n",len,ret);
    recv_usart_package(USART_FIRST_TYPE, (u8 *) cn_frist_uart->rx_buf, len);
#else
//    printf("recv_usart1_package:%d \r\n",cn_frist_uart->rx_data_len);
    if(cn_frist_uart->com_contorl_flag == 0) 
    {
         return WM_FAILED;
    }
    if (cn_frist_uart->rx_data_len  == 0)
    {
        memset(cn_frist_uart->rx_buf, 0, (len + 1));
        if (usart1_recv_timeout  != cn_frist_uart->recv_overtime)
        {
             cn_frist_uart->recv_overtime = usart1_recv_timeout;
            tls_timer_change(usart1_timerout_id,cn_frist_uart->recv_overtime );
        }
        else
        {
            tls_timer_start(usart1_timerout_id);
        }
    }
    ret = tls_uart_read(TLS_UART_1, (u8 *) &cn_frist_uart->rx_buf[cn_frist_uart->rx_data_len ], len);  /* input */
    if (ret <= 0)
    {
        return 0;
    }
    cn_frist_uart->rx_data_len += ret;
#endif
#else
    cn_frist_uart->rx_data_len += len;
    if (cn_frist_uart->rx_msg_num < 3)
    {
        cn_frist_uart->rx_msg_num++;
        tls_os_queue_send(cn_frist_uart->cn_uart_q, (void *) DEMO_MSG_UART_RECEIVE_DATA, 0);
    }
#endif
    return WM_SUCCESS;
}

static void cn_frist_uart_task(void *sdata)
{
    CN_USART_ST *uart = (CN_USART_ST *) sdata;
    cn_uart_options_t opt;
    void *msg;
    int ret = 0;
    int len = 0;
    int rx_len = 0;

    for (;;)
    {
        tls_os_queue_receive(uart->cn_uart_q, (void **) &msg, 0, 0);
        switch ((u32) msg)
        {
        case DEMO_MSG_OPEN_UART:
        {
            opt.baudrate = uart->bandrate;
            opt.paritytype = uart->parity;
            opt.stopbits = uart->stopbits;
            opt.charlength = uart->charlength;
            opt.flow_ctrl = uart->flow_ctrl;
            //选择待使用的引脚及具体的复用功能
            /* UART1_RX-PB07  UART1_TX-PB06 */
            wm_uart1_rx_config(WM_IO_PB_07);
            wm_uart1_tx_config(WM_IO_PB_06);

            if (WM_SUCCESS != tls_uart_port_init(TLS_UART_1, &opt, 0))
            {
                printf("uart1 init error\n");
            }

            tls_uart_rx_callback_register((u16) TLS_UART_1, (s16(*)(u16, void*))cn_frist_uart_rx, NULL);
            tls_uart_tx_callback_register(TLS_UART_1, (s16(*)(struct tls_uart_port *))uart_tx_sent_callback);
        }
        break;

        case DEMO_MSG_UART_RECEIVE_DATA:
        {
            rx_len = uart->rx_data_len;
            while (rx_len > 0)
            {
                len = (rx_len > CN_UART_RX_BUF_SIZE) ? CN_UART_RX_BUF_SIZE : rx_len;
                memset(uart->rx_buf, 0, (CN_UART_RX_BUF_SIZE + 1));
                ret = tls_uart_read(TLS_UART_1, (u8 *) uart->rx_buf, len);  /* input */
                if (ret <= 0)
                {
                    break;
                }
               printf("recv_usart1_package:%d,%d \r\n",rx_len,ret);
               recv_usart_package(USART_FIRST_TYPE, (u8 *) uart->rx_buf, ret);
                rx_len -= ret;
                uart->rx_data_len -= ret;
            }
            if (uart->rx_msg_num > 0)
            {
                uart->rx_msg_num--;
            }
        }
        break;

        default:
            break;
        }
    }
}

//串口2回调
static s16 cn_second_uart_rx(u16 len)
{
    int ret;
    if (NULL == cn_second_uart)
    {
        return WM_FAILED;
    }
    if(cn_second_uart->com_contorl_flag == 0) 
    {
         return WM_FAILED;
    }
#if 1

#if 0
    memset(cn_forth_uart->rx_buf, 0, (len + 1));
    ret = tls_uart_read(TLS_UART_2, (u8 *) cn_forth_uart->rx_buf, len);  /* input */
    if (ret <= 0)
    {
        return 0;
    }
    printf("recv_usart2_package:%d,%d \r\n",len,ret);
    recv_usart_package(USART_SECOND_TYPE, (u8 *) cn_second_uart->rx_buf, len);
#else
 //   printf("recv_usart2_package:%d \r\n",cn_second_uart->rx_data_len);
    if (cn_second_uart->rx_data_len  == 0)
    {
        memset(cn_second_uart->rx_buf, 0, (len + 1));
        if (usart2_recv_timeout  != cn_second_uart->recv_overtime)
        {
             cn_second_uart->recv_overtime = usart2_recv_timeout;
            tls_timer_change(usart2_timerout_id,cn_second_uart->recv_overtime );
        }
        else
        {
            tls_timer_start(usart2_timerout_id);
        }
    }
    ret = tls_uart_read(TLS_UART_2, (u8 *) &cn_second_uart->rx_buf[cn_second_uart->rx_data_len ], len);  /* input */
    if (ret <= 0)
    {
        return 0;
    }
    cn_second_uart->rx_data_len += ret;

#endif
#else
    cn_second_uart->rx_data_len += len;
    if (cn_second_uart->rx_msg_num < 3)
    {
        cn_second_uart->rx_msg_num++;
        tls_os_queue_send(cn_second_uart->cn_uart_q, (void *) DEMO_MSG_UART_RECEIVE_DATA, 0);
    }
#endif
    return WM_SUCCESS;
}

static void cn_second_uart_task(void *sdata)
{
    CN_USART_ST *uart = (CN_USART_ST *) sdata;
    cn_uart_options_t opt;
    void *msg;
    int ret = 0;
    int len = 0;
    int rx_len = 0;

    for (;;)
    {
        tls_os_queue_receive(uart->cn_uart_q, (void **) &msg, 0, 0);
        switch ((u32) msg)
        {
        case DEMO_MSG_OPEN_UART:
        {
            opt.baudrate = uart->bandrate;
            opt.paritytype = uart->parity;
            opt.stopbits = uart->stopbits;
            opt.charlength = TLS_UART_CHSIZE_8BIT;
            opt.flow_ctrl = TLS_UART_FLOW_CTRL_NONE;

            //选择待使用的引脚及具体的复用功能
            /* UART2_RX-PB03  UART2_TX-PB02 */
            wm_uart2_rx_config(WM_IO_PB_03);
            wm_uart2_tx_scio_config(WM_IO_PB_02);

            if (WM_SUCCESS != tls_uart_port_init(TLS_UART_2, &opt, 0))
            {
                printf("uart1 init error\n");
            }

            tls_uart_rx_callback_register((u16) TLS_UART_2, (s16(*)(u16, void*))cn_second_uart_rx, NULL);
            tls_uart_tx_callback_register(TLS_UART_2, (s16(*)(struct tls_uart_port *))uart_tx_sent_callback);
        }
        break;

        case DEMO_MSG_UART_RECEIVE_DATA:
        {
            rx_len = uart->rx_data_len;
            while (rx_len > 0)
            {
                len = (rx_len > CN_UART_RX_BUF_SIZE) ? CN_UART_RX_BUF_SIZE : rx_len;
                memset(uart->rx_buf, 0, (CN_UART_RX_BUF_SIZE + 1));
                ret = tls_uart_read(TLS_UART_2, (u8 *) uart->rx_buf, len);  /* input */
                if (ret <= 0)
                {
                    break;
                }
                printf("recv_usart2_package:%d,%d \r\n",rx_len,ret);
               recv_usart_package(USART_SECOND_TYPE, (u8 *) uart->rx_buf, ret);
                rx_len -= ret;
                uart->rx_data_len -= ret;
            }
            if (uart->rx_msg_num > 0)
            {
                uart->rx_msg_num--;
            }
        }
        break;

        default:
            break;
        }
    }
}

//串口3回调
static s16 cn_third_uart_rx(u16 len)
{
    int ret;
    if (NULL == cn_third_uart)
    {
        return WM_FAILED;
    }
    if(cn_third_uart->com_contorl_flag == 0) 
    {
         return WM_FAILED;
    }
#if 1

#if 0
    memset(cn_forth_uart->rx_buf, 0, (len + 1));
    ret = tls_uart_read(TLS_UART_3, (u8 *) cn_forth_uart->rx_buf, len);  /* input */
    if (ret <= 0)
    {
        return 0;
    }
    printf("recv_usart3_package:%d,%d \r\n",len,ret);
    recv_usart_package(USART_THIRD_TYPE, (u8 *) cn_third_uart->rx_buf, len);
#else
 //   printf("recv_usart3_package:%d \r\n",cn_third_uart->rx_data_len);
    if (cn_third_uart->rx_data_len  == 0)
    {
        memset(cn_third_uart->rx_buf, 0, (len + 1));
        if (usart3_recv_timeout  != cn_third_uart->recv_overtime)
        {
             cn_third_uart->recv_overtime = usart3_recv_timeout;
            tls_timer_change(usart3_timerout_id,cn_third_uart->recv_overtime );
        }
        else
        {
            tls_timer_start(usart3_timerout_id);
        }
    }
    ret = tls_uart_read(TLS_UART_3, (u8 *) &cn_third_uart->rx_buf[cn_third_uart->rx_data_len ], len);  /* input */
    if (ret <= 0)
    {
        return 0;
    }
    cn_third_uart->rx_data_len += ret;
#endif
#else
    cn_third_uart->rx_data_len += len;
    if (cn_third_uart->rx_msg_num < 3)
    {
        cn_third_uart->rx_msg_num++;
        tls_os_queue_send(cn_third_uart->cn_uart_q, (void *) DEMO_MSG_UART_RECEIVE_DATA, 0);
    }
#endif
    return WM_SUCCESS;
}

static void cn_third_uart_task(void *sdata)
{
    CN_USART_ST *uart = (CN_USART_ST *) sdata;
    cn_uart_options_t opt;
    void *msg;
    int ret = 0;
    int len = 0;
    int rx_len = 0;

    for (;;)
    {
        tls_os_queue_receive(uart->cn_uart_q, (void **) &msg, 0, 0);
        switch ((u32) msg)
        {
        case DEMO_MSG_OPEN_UART:
        {
            opt.baudrate = uart->bandrate;
            opt.paritytype = uart->parity;
            opt.stopbits = uart->stopbits;
            opt.charlength = TLS_UART_CHSIZE_8BIT;
            opt.flow_ctrl = TLS_UART_FLOW_CTRL_NONE;

            //选择待使用的引脚及具体的复用功能
            /* UART3_RX-PB01  UART3_TX-PB00 */
            wm_uart3_rx_config(WM_IO_PB_01);
            wm_uart3_tx_config(WM_IO_PB_00);

            if (WM_SUCCESS != tls_uart_port_init(TLS_UART_3, &opt, 0))
            {
                printf("uart1 init error\n");
            }

            tls_uart_rx_callback_register((u16) TLS_UART_3, (s16(*)(u16, void*))cn_third_uart_rx, NULL);
            tls_uart_tx_callback_register(TLS_UART_3, (s16(*)(struct tls_uart_port *))uart_tx_sent_callback);
        }
        break;

        case DEMO_MSG_UART_RECEIVE_DATA:
        {
            rx_len = uart->rx_data_len;
            while (rx_len > 0)
            {
                len = (rx_len > CN_UART_RX_BUF_SIZE) ? CN_UART_RX_BUF_SIZE : rx_len;
                memset(uart->rx_buf, 0, (CN_UART_RX_BUF_SIZE + 1));
                ret = tls_uart_read(TLS_UART_3, (u8 *) uart->rx_buf, len);  /* input */
                if (ret <= 0)
                {
                    break;
                }
                printf("recv_usart3_package:%d,%d \r\n",rx_len,ret);
                recv_usart_package(USART_THIRD_TYPE, (u8 *) uart->rx_buf, ret);
                rx_len -= ret;
                uart->rx_data_len -= ret;
            }
            if (uart->rx_msg_num > 0)
            {
                uart->rx_msg_num--;
            }
        }
        break;

        default:
            break;
        }
    }
}

//串口4回调
static s16 cn_forth_uart_rx(u16 len)
{
    int ret;
    if (NULL == cn_forth_uart)
    {
        return WM_FAILED;
    }
    if(cn_forth_uart->com_contorl_flag == 0) 
    {
         return WM_FAILED;
    }
#if 1

#if 0
    memset(cn_forth_uart->rx_buf, 0, (len + 1));
    ret = tls_uart_read(TLS_UART_4, (u8 *) cn_forth_uart->rx_buf, len);  /* input */
    if (ret <= 0)
    {
        return 0;
    }
    printf("recv_usart4_package:%d,%d \r\n",len,ret);
    recv_usart_package(USART_FORTH_TYPE, (u8 *) cn_forth_uart->rx_buf, len);
#else
  //  printf("recv_usart4_package:%d \r\n",cn_forth_uart->rx_data_len);
    if (cn_forth_uart->rx_data_len  == 0)
    {
        memset(cn_forth_uart->rx_buf, 0, (len + 1));
        if (usart4_recv_timeout  != cn_forth_uart->recv_overtime)
        {
             cn_forth_uart->recv_overtime = usart4_recv_timeout;
            tls_timer_change(usart4_timerout_id,cn_forth_uart->recv_overtime );
        }
        else
        {
            tls_timer_start(usart4_timerout_id);
        }
    }
    ret = tls_uart_read(TLS_UART_4, (u8 *) &cn_forth_uart->rx_buf[cn_forth_uart->rx_data_len ], len);  /* input */
    if (ret <= 0)
    {
        return 0;
    }
    cn_forth_uart->rx_data_len += ret;

#endif
#else
    cn_forth_uart->rx_data_len += len;
    if (cn_forth_uart->rx_msg_num < 3)
    {
        cn_forth_uart->rx_msg_num++;
        tls_os_queue_send(cn_forth_uart->cn_uart_q, (void *) DEMO_MSG_UART_RECEIVE_DATA, 0);
    }
#endif
    return WM_SUCCESS;
}

static void cn_forth_uart_task(void *sdata)
{
    CN_USART_ST *uart = (CN_USART_ST *) sdata;
    cn_uart_options_t opt;
    void *msg;
    int ret = 0;
    int len = 0;
    int rx_len = 0;

    for (;;)
    {
        tls_os_queue_receive(uart->cn_uart_q, (void **) &msg, 0, 0);
        switch ((u32) msg)
        {
        case DEMO_MSG_OPEN_UART:
        {
            opt.baudrate = uart->bandrate;
            opt.paritytype = uart->parity;
            opt.stopbits = uart->stopbits;
            opt.charlength = TLS_UART_CHSIZE_8BIT;
            opt.flow_ctrl = TLS_UART_FLOW_CTRL_NONE;

            //选择待使用的引脚及具体的复用功能
            /* UART4_RX-PB04  UART4_TX-PB04 */
            wm_uart4_rx_config(WM_IO_PB_05);
            wm_uart4_tx_config(WM_IO_PB_04);

            if (WM_SUCCESS != tls_uart_port_init(TLS_UART_4, &opt, 0))
            {
                printf("uart1 init error\n");
            }

            tls_uart_rx_callback_register((u16) TLS_UART_4, (s16(*)(u16, void*))cn_forth_uart_rx, NULL);
            tls_uart_tx_callback_register(TLS_UART_4, (s16(*)(struct tls_uart_port *))uart_tx_sent_callback);
        }
        break;

        case DEMO_MSG_UART_RECEIVE_DATA:
        {
            rx_len = uart->rx_data_len;
            while (rx_len > 0)
            {
                len = (rx_len > CN_UART_RX_BUF_SIZE) ? CN_UART_RX_BUF_SIZE : rx_len;
                memset(uart->rx_buf, 0, (CN_UART_RX_BUF_SIZE + 1));
                ret = tls_uart_read(TLS_UART_4, (u8 *) uart->rx_buf, len);  /* input */
                if (ret <= 0)
                {
                    break;
                }
                printf("recv_usart4_package:%d,%d \r\n",rx_len,ret);
                recv_usart_package(USART_FORTH_TYPE, (u8 *) uart->rx_buf, ret);
                rx_len -= ret;
                uart->rx_data_len -= ret;
            }
            if (uart->rx_msg_num > 0)
            {
                uart->rx_msg_num--;
            }
        }
        break;

        default:
            break;
        }
    }
}

int set_usart1_config(cn_uart_options_t  *ops)
{
    struct tls_timer_cfg timer_cfg;
    static  time_flag = 0;
    printf("\nuart1 param=%d, %d, %d\n",ops->baudrate,ops->paritytype , ops->stopbits);
    if (time_flag == 0)
    {
        if (NULL == cn_frist_uart)
        {
            cn_frist_uart = tls_mem_alloc(sizeof(CN_USART_ST));
            if (NULL == cn_frist_uart)
            {
                goto _error;
            }
            memset(cn_frist_uart, 0, sizeof(CN_USART_ST));

            cn_frist_uart->rx_buf = tls_mem_alloc(CN_UART_RX_BUF_SIZE + 1);
            if (NULL == cn_frist_uart->rx_buf)
            {
                goto _error1;
            }
            tls_os_queue_create(&(cn_frist_uart->cn_uart_q), DEMO_QUEUE_SIZE);
            tls_os_task_create(NULL, NULL,
                            cn_frist_uart_task,
                            (void *) cn_frist_uart,
                            (void *) cn_frist_uart_task_stk, /** 任务栈的起始地址 */
                            CN_UART_TAST_STK_SIZE,                         /** 任务栈的大小     */
                            DEMO_UART_TASK_PRIO, 0);
        }
        time_flag = 1;
        timer_cfg.unit = TLS_TIMER_UNIT_MS;
        timer_cfg.timeout = RECV_TIMEOUT;
        timer_cfg.is_repeat = 1;
        timer_cfg.callback = (tls_timer_irq_callback)usart1_timerout_irq;
        timer_cfg.arg = NULL;
        usart1_timerout_id = tls_timer_create(&timer_cfg);
        cn_frist_uart->recv_overtime = RECV_TIMEOUT;
    }
    usart1_recv_timeout = ops->recv_overtime;

    cn_frist_uart->com_contorl_flag = 1;
    cn_frist_uart->bandrate = ops->baudrate;
    cn_frist_uart->charlength = ops->charlength;
    cn_frist_uart->flow_ctrl = ops->flow_ctrl;
    cn_frist_uart->parity = ops->paritytype;
    cn_frist_uart->stopbits = ops->stopbits;
    cn_frist_uart->rx_msg_num = 0;
    cn_frist_uart->rx_data_len = 0;
    tls_os_queue_send(cn_frist_uart->cn_uart_q, (void *) DEMO_MSG_OPEN_UART, 0);

    return WM_SUCCESS;

_error1:
    tls_mem_free(cn_frist_uart);
    cn_frist_uart = NULL;
_error:
    printf("\nmem error\n");
    return WM_FAILED;
}

int set_usart1_colse(void)
{
    tls_timer_stop(usart1_timerout_id);
    cn_frist_uart->rx_data_len = 0;
    cn_frist_uart->com_contorl_flag = 0;
    return 0;
}

int set_usart2_config(cn_uart_options_t  *ops)
{
    struct tls_timer_cfg timer_cfg;
    static  time_flag = 0;
    printf("\nuart2 param=%d, %d, %d\n",ops->baudrate,ops->paritytype , ops->stopbits);
    if(time_flag == 0)
    {
        if (NULL == cn_second_uart)
        {
            cn_second_uart = tls_mem_alloc(sizeof(CN_USART_ST));
            if (NULL == cn_second_uart)
            {
                goto _error;
            }
            memset(cn_second_uart, 0, sizeof(CN_USART_ST));

            cn_second_uart->rx_buf = tls_mem_alloc(CN_UART_RX_BUF_SIZE + 1);
            if (NULL == cn_second_uart->rx_buf)
            {
                goto _error1;
            }
            tls_os_queue_create(&(cn_second_uart->cn_uart_q), DEMO_QUEUE_SIZE);
            tls_os_task_create(NULL, NULL,
                            cn_second_uart_task,
                            (void *) cn_second_uart,
                            (void *) cn_second_uart_task_stk, /** 任务栈的起始地址 */
                            CN_UART_TAST_STK_SIZE,                         /** 任务栈的大小     */
                            DEMO_UART_TASK_PRIO, 0);
        }      
        time_flag = 1;
        timer_cfg.unit = TLS_TIMER_UNIT_MS;
        timer_cfg.timeout = RECV_TIMEOUT;
        timer_cfg.is_repeat = 1;
        timer_cfg.callback = (tls_timer_irq_callback)usart2_timerout_irq;
        timer_cfg.arg = NULL;
        usart2_timerout_id = tls_timer_create(&timer_cfg);
         cn_second_uart->recv_overtime = RECV_TIMEOUT;
    }
    usart2_recv_timeout = ops->recv_overtime;
    cn_second_uart->com_contorl_flag = 1;
    cn_second_uart->bandrate = ops->baudrate;
    cn_second_uart->charlength = ops->charlength;
    cn_second_uart->flow_ctrl = ops->flow_ctrl;
    cn_second_uart->parity = ops->paritytype;
    cn_second_uart->stopbits = ops->stopbits;
    cn_second_uart->rx_msg_num = 0;
    cn_second_uart->rx_data_len = 0;
    tls_os_queue_send(cn_second_uart->cn_uart_q, (void *) DEMO_MSG_OPEN_UART, 0);

    return WM_SUCCESS;

_error1:
    tls_mem_free(cn_second_uart);
    cn_second_uart = NULL;
_error:
    printf("\nmem error\n");
    return WM_FAILED;
}

int set_usart2_colse(void)
{
    tls_timer_stop(usart2_timerout_id);
    cn_second_uart->rx_data_len = 0;
    cn_second_uart->com_contorl_flag = 0;
        return 0;
}

int set_usart3_config(cn_uart_options_t  *ops)
{
    struct tls_timer_cfg timer_cfg;
    static  time_flag = 0;
    printf("\nuart3 param=%d, %d, %d\n",ops->baudrate,ops->paritytype , ops->stopbits);
    if(time_flag == 0)
    {
       if (NULL == cn_third_uart)
        {
            cn_third_uart = tls_mem_alloc(sizeof(CN_USART_ST));
            if (NULL == cn_third_uart)
            {
                goto _error;
            }
            memset(cn_third_uart, 0, sizeof(CN_USART_ST));

            cn_third_uart->rx_buf = tls_mem_alloc(CN_UART_RX_BUF_SIZE + 1);
            if (NULL == cn_third_uart->rx_buf)
            {
                goto _error1;
            }
            tls_os_queue_create(&(cn_third_uart->cn_uart_q), DEMO_QUEUE_SIZE);
            tls_os_task_create(NULL, NULL,
                            cn_third_uart_task,
                            (void *) cn_third_uart,
                            (void *) cn_third_uart_task_stk, /** 任务栈的起始地址 */
                            CN_UART_TAST_STK_SIZE,                         /** 任务栈的大小     */
                            DEMO_UART_TASK_PRIO, 0);
        }     
        time_flag = 1;
        timer_cfg.unit = TLS_TIMER_UNIT_MS;
        timer_cfg.timeout = RECV_TIMEOUT;
        timer_cfg.is_repeat = 1;
        timer_cfg.callback = (tls_timer_irq_callback)usart3_timerout_irq;
        timer_cfg.arg = NULL;
        usart3_timerout_id = tls_timer_create(&timer_cfg);
        cn_third_uart->recv_overtime = RECV_TIMEOUT;
    }
    usart3_recv_timeout = ops->recv_overtime;
    cn_third_uart->com_contorl_flag = 1;
    cn_third_uart->bandrate = ops->baudrate;
    cn_third_uart->charlength = ops->charlength;
    cn_third_uart->flow_ctrl = ops->flow_ctrl;
    cn_third_uart->parity = ops->paritytype;
    cn_third_uart->stopbits = ops->stopbits;
    cn_third_uart->rx_msg_num = 0;
    cn_third_uart->rx_data_len = 0;
    tls_os_queue_send(cn_third_uart->cn_uart_q, (void *) DEMO_MSG_OPEN_UART, 0);
    return WM_SUCCESS;

_error1:
    tls_mem_free(cn_third_uart);
    cn_third_uart = NULL;
_error:
    printf("\nmem error\n");
    return WM_FAILED;
}

int set_usart3_colse(void)
{
    tls_timer_stop(usart3_timerout_id);
    cn_third_uart->rx_data_len = 0;
    cn_third_uart->com_contorl_flag  = 0;
        return 0;
}

int set_usart4_config(cn_uart_options_t  *ops)
{
    struct tls_timer_cfg timer_cfg;
    static  time_flag = 0;
    printf("\nuart4 param=%d, %d, %d\n",ops->baudrate,ops->paritytype , ops->stopbits);
    if (time_flag == 0)
    {
        time_flag = 1;
        if (NULL == cn_forth_uart)
        {
            cn_forth_uart = tls_mem_alloc(sizeof(CN_USART_ST));
            if (NULL == cn_forth_uart)
            {
                goto _error;
            }
            memset(cn_forth_uart, 0, sizeof(CN_USART_ST));

            cn_forth_uart->rx_buf = tls_mem_alloc(CN_UART_RX_BUF_SIZE + 1);
            if (NULL == cn_forth_uart->rx_buf)
            {
                goto _error1;
            }
            tls_os_queue_create(&(cn_forth_uart->cn_uart_q), DEMO_QUEUE_SIZE);
            tls_os_task_create(NULL, NULL,
                                cn_forth_uart_task,
                                (void *) cn_forth_uart,
                                (void *) cn_forth_uart_task_stk, /** 任务栈的起始地址 */
                                CN_UART_TAST_STK_SIZE,                         /** 任务栈的大小     */
                                DEMO_UART_TASK_PRIO, 0);
        }
        struct tls_timer_cfg timer_cfg;
        timer_cfg.unit = TLS_TIMER_UNIT_MS;
        timer_cfg.timeout = RECV_TIMEOUT;
        timer_cfg.is_repeat = 1;
        timer_cfg.callback = (tls_timer_irq_callback)usart4_timerout_irq;
        timer_cfg.arg = NULL;
        usart4_timerout_id = tls_timer_create(&timer_cfg);
        cn_forth_uart->recv_overtime = RECV_TIMEOUT;
    }
    usart4_recv_timeout = ops->recv_overtime;
    cn_forth_uart->com_contorl_flag = 1;
    cn_forth_uart->bandrate = ops->baudrate;
    cn_forth_uart->charlength = ops->charlength;
    cn_forth_uart->flow_ctrl = ops->flow_ctrl;
    cn_forth_uart->parity = ops->paritytype;
    cn_forth_uart->stopbits = ops->stopbits;
    cn_forth_uart->rx_msg_num = 0;
    cn_forth_uart->rx_data_len = 0;
    tls_os_queue_send(cn_forth_uart->cn_uart_q, (void *) DEMO_MSG_OPEN_UART, 0);
    return WM_SUCCESS;

_error1:
    tls_mem_free(cn_forth_uart);
    cn_forth_uart = NULL;
_error:
    printf("\nmem error\n");
    return WM_FAILED;
}

int set_usart4_colse(void)
{
    tls_timer_stop(usart4_timerout_id);
    cn_forth_uart->rx_data_len = 0;
    cn_forth_uart->com_contorl_flag = 0;
    return 0;
}

int set_all_usart_config(cn_uart_options_t  *ops)
{
     set_usart1_config(ops);
    set_usart2_config(ops);
    set_usart3_config(ops);
    set_usart4_config(ops);
}

int cn_uart_write(u8 type, char *buf, u16 lenth)
{
    switch (type)
    {
    case  1:
            tls_uart_write(TLS_UART_1, buf, lenth); 
        break;

     case  2:
            tls_uart_write(TLS_UART_2, buf, lenth); 
        break;

    case  3:
            tls_uart_write(TLS_UART_3, buf, lenth); 
        break;

    case  4:
            tls_uart_write(TLS_UART_4, buf, lenth); 
        break;

    case  0xFF:
            tls_uart_write(TLS_UART_1, buf, lenth); 
            tls_uart_write(TLS_UART_2, buf, lenth); 
            tls_uart_write(TLS_UART_3, buf, lenth); 
            tls_uart_write(TLS_UART_4, buf, lenth);           
        break;

    default:
        break;
    }
}